#import "GeneralScene.h"
#import "LevelCompleteScene.h"
#import "GameOverScene.h"
#import "GameManager.h"
#import "PListManager.h"

@implementation GeneralScene
@synthesize answerArray;
@synthesize layer, hudLayer;
@synthesize totalTime, remainingTime, penaltyTime;
@synthesize country;
@synthesize currentWave, totalWave, currentLevel;


-(id) init
{
    if( (self = [super init] ) )
    {   
		//Get Device Size
		CGSize winSize = [[CCDirector sharedDirector] winSize];
		
		//Start init variable
        self.currentLevel = [[GameManager sharedGameManager] currentLevel];
		[self setCurrentWave:1];
		
		//Get value from pList and init member variable
		PListManager *plistManager = [PListManager openFile:@"LevelInfo"];
		
		NSString *strKeyName = [NSString stringWithFormat:@"Level%d", currentLevel];
		CCArray *pathArray = [CCArray arrayWithCapacity:1];
		[pathArray addObject:strKeyName];
		
		NSMutableDictionary *dict = [plistManager getAllValuesByPath:pathArray];
		for(int i=0; i < [[dict allKeys] count]; i++)
		{
			NSString *key = (NSString *)[[dict allKeys] objectAtIndex:i];
			if([key isEqualToString:@"TotalTime"])
			{
				self.totalTime = [[dict valueForKey:key] floatValue];
			}
			else if([key isEqualToString:@"PenaltyTime"])
			{
				self.penaltyTime = [[dict valueForKey:key] floatValue];
			}
			else if([key isEqualToString:@"Country"])
			{
				self.country = [dict valueForKey:key];
			}
			else if([key isEqualToString:@"TotalWave"])
			{
				self.totalWave = [[dict valueForKey:key] intValue];
			}
		}
		
		[plistManager closeFile];
	}
    
    return self;
}

-(void) dealloc
{
    [[GameManager sharedGameManager] setGameState:kGAME_STATE_NIL];
    [answerArray release];
    [country release];
	[layer release];
    [super dealloc];
}


-(NSMutableArray *) genAnswer:(int)count min:(int)minVal max:(int)maxVal
{
    int iInterval = maxVal - minVal + 1;
    answerArray = [[NSMutableArray alloc] initWithCapacity:count];

    for(int i=0; i<count; i++)
    {
        do {
            int iNewVal = minVal + arc4random() % iInterval;
            NSNumber *number = [NSNumber numberWithInt:iNewVal];
            
            //If not contains, then add Object in it
            bool bIsRepeated = false;
            for(int j=0; j<i; j++)
            {
                if( iNewVal == [[answerArray objectAtIndex:j] intValue] )
                {
                    bIsRepeated = true;
                    break;
                }
            }
            if( !bIsRepeated )
            {
                [answerArray addObject:number];
                break;
            }
            
        } while (true);
    }

	//Sort Array
    self.answerArray = (NSMutableArray *)[answerArray sortedArrayUsingSelector:@selector(compare:)];
    return self.answerArray;
    
}
/*
 -(void) sortNumber
{    
    NSArray *newArr = [[arrGeneratedNumber getNSArray] sortedArrayUsingSelector:@selector(compare:)];
    self.arrGeneratedNumber = [CCArray arrayWithNSArray:newArr];
}
 
-(void) startGame
{

}
*/

-(void) startLevel
{
	//Ask Layer startLevel, any init operation
	[layer startLevel];
	
	//Start Wave
	[self startWave:1];
}

-(void) startWave:(int)wave
{
    CCLOG(@"startWave Should be inherited");
    [self doesNotRecognizeSelector:_cmd];
}

-(void) correctAnswer:(GeneralSprite *)sprite
{
	
}

-(void) incorrectAnswer:(GeneralSprite *)sprite
{
	
}

-(bool) isLevelCompleted
{  
    if(currentWave > totalWave)
	{
        return YES;
    }
	return NO;
}

-(bool) isWaveCompleted
{
    //All numbers are tapped
    if([answerAarray count] != 0)
    {
        return NO;
    }
/*
    //All Sprites are removed
    if([layer remainSpriteCount] != 0)
    {
        return NO;
    }
*/
    currentWave++;
    return YES;
}


#pragma mark GeneralLayerDelegate
-(void) checkAnswer:(GeneralSprite *)sprite
{
	int number = [sprite number];
    //Always get the first object from the array (a.k.a, the smallest number in the array
    int correctAns = [[answerAarray objectAtIndex:0] intValue];
    if( correctAns == number )
    {
        [answerAarray removeObjectAtIndex:0]; //Remove the first object
		[self correctAnswer:sprite];
    }
    else
    {
		[self incorrectAnswer:sprite];
    }
}

-(void) didCompleteWave
{
	
}

-(void) didCompleteLevel
{
	
}

-(void) completePerformCorrectAnswer:(CCSprite *)sprite
{
	
}

-(void) completePerformIncorrectAnswer:(CCSprite *)sprite
{
	
}

#pragma mark HUDLayerDelegate
-(void) timesUp
{
	
}

@end

